package com.example.qinghubeta.exp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cunoraz.gifview.library.GifView;
import com.example.qinghubeta.R;

public class Exp1 extends AppCompatActivity implements View.OnClickListener {


    SharedPreferences sharedPreferences;
    TextView dial,title,success;
    ImageView gateway,light,computer,power,chehui,chehui1,ok,dialcon,begin,dianyuan1,wangguan1,guangzhao1,diannao1;
    Button opower,ocomputer,ogateway;
    TextView ma,lux,lux1;
    CountDownTimer countDownTimer1,seeer,duihua;
    GifView gif3;
    int falg=1;
    int gzflag=0,wgflag=0,dyflag=0,dnflag=0;
    int i11=0;
    int i=1;
    int state =1;
    int i22=0,i33=0,i44=0,i55=0,i66=0;
    int gifstate=0;
    String text1= "光-照-传-感-器-是-一-种-传-感-器-，-光-照-强-度-，-简-称-照-度-，-工-作-原-理-是-将-光-照-强-度-值-转-为-电-压-值-，-主-要-用-于-农-业-林-业-温-室-大-棚-培-育-等-。--.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ";
    String text2= "请-观-看-动-画-，-当-光-照-传-感-器-接-通-电-源-后-，-光-照-传-感-器-开-始-工-作-，-把-采-集-到-的-值-传-到-物-联-网-中-心-网-关-，-再-从-电-脑-上-读-取-传-感-器-采-集-到-的-值-.- - - - - - - - - ";
    String text3= "那-么-传-感-器-内-部-到-底-发-生-了-怎-样-的-变-化-呢-？-让-我-们-一-起-进-入-传-感-器-内-部-看-一-下-吧-！- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
    String text4= "通-过-动-画-我-们-可-以-看-到-光-感-原-件-采-集-到-了-光-照-值-，-光-照-值-越-大-，-里-面-的-粒-子-运-动-的-越-快-，-通-过-电-阻-的-电-流-就-越-大-，-反-之-则-越-小-  - - - - - - - - - - - - - - - - - - - - - - - - - - - - ";
    String text5= "通-过-动-画-我-们-得-知-，-光-照-传-感-器-传-输-的-电-流-到-了-网-关-之-后-，-通-过-计-算-公-式-得-到-了-我-们-需-要-的-值-，-电-流-越-大-，-光-照-值-越-大-，-电-流-越-小-，-光-照-值-越-小-，-所-以-整-个-的-传-输-链-路-为-电-源- -》-传-感-器- -》-网-关- -》-终-端-设-备- - - - - - - - - - -  - - - - - - - - ";
    String text6= "现-在-请-同-学-们-自-己-做-一-下-试-试-吧-！-";
    String[] textarray = text1.split("-");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exp1);
        initview();
        setClick();
        jishiqi();
        jishiqistart();
        setvisible();

    }

    private void setvisible() {
        success.setVisibility(View.INVISIBLE);
        dial.setVisibility(View.INVISIBLE);
        diannao1.setVisibility(View.INVISIBLE);
        dianyuan1.setVisibility(View.INVISIBLE);
        guangzhao1.setVisibility(View.INVISIBLE);
        wangguan1.setVisibility(View.INVISIBLE);
        ogateway.setVisibility(View.INVISIBLE);
        ocomputer.setVisibility(View.INVISIBLE);
        opower.setVisibility(View.INVISIBLE);
        lux.setVisibility(View.INVISIBLE);
        lux1.setVisibility(View.INVISIBLE);
        ma.setVisibility(View.INVISIBLE);

    }

    private void jishiqistart() {
        seeer.start();
        countDownTimer1.start();
    }

    private void jishiqi() {
        duihua = new CountDownTimer(10000000,105) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long l) {
                //1
                if (i11==textarray.length-1){
                    state=2;
                    i11+=1;
                    dial.setText("");
                    textarray = null;
                    textarray = text2.split("-");
                }else if (state ==1){
                    gifstate = 11;
                    dial.setText(dial.getText().toString() + textarray[i11]);
                    i11 = i11 + 1;
                }
                //2
                if (state ==2){
                    if (i22==textarray.length-1){
                        state =3;
                        i22+=1;
                        dial.setText("");
                        textarray = null;
                        textarray = text3.split("-");
                    }else if (state == 2){
                        dial.setText(dial.getText().toString() + textarray[i22]);
                        i22 = i22 + 1;
                    }
                }

                //3
                if (state ==3){
                    if (i33==textarray.length-1){
                        gifstate =21;
                        state =4;
                        i33+=1;
                        dial.setText("");
                        textarray = null;
                        textarray = text4.split("-");
                    }else if (state == 3){
                        dial.setText(dial.getText().toString() + textarray[i33]);
                        i33= i33 + 1;
                    }
                }

                //4
                if (state ==4){
                    if (i44==textarray.length-1){
                        gifstate =31;
                        state =5;
                        i44+=1;
                        dial.setText("");
                        textarray = null;
                        textarray = text5.split("-");
                    }else if (state == 4){
                        dial.setText(dial.getText().toString() + textarray[i44]);
                        i44= i44 + 1;
                    }
                }

                //5
                if (state ==5){
                    if (i55==textarray.length-1){
                        state =6;
                        i22+=1;
                        dial.setText("");
                        textarray = null;
                        textarray = text6.split("-");
                    }else if (state == 5    ){
                        dial.setText(dial.getText().toString() + textarray[i55]);
                        i55 = i55 + 1;
                    }
                }
                //6
                if (state ==6){
                    try {
                        if (i66==textarray.length-1){
                            i66+=1;
                            gif3.setVisibility(View.INVISIBLE);
                            duihua.cancel();
                        }else if (state == 6){
                            dial.setText(dial.getText().toString() + textarray[i66]);
                            i66 = i66 + 1;
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }



            }

            @Override
            public void onFinish() {

            }
        };
        seeer = new CountDownTimer(99999999,1) {
            @Override
            public void onTick(long l) {
                if (gifstate == 21){
                    countDownTimer1.start();
                }

                if (ma.getText().equals("15ma") && lux.getText().equals("506lux")  && lux1.getText().equals("506lux")){
                    success.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFinish() {

            }
        };
        countDownTimer1 = new CountDownTimer(9999999,1) {
            @Override
            public void onTick(long l) {
                System.out.println(gifstate);
                if (gifstate == 11) {
                    gif3.setGifResource(R.drawable.gif1);
                    countDownTimer1.cancel();
                    gifstate =10;
                }
                if (gifstate == 21){

                    gif3.setGifResource(R.drawable.gif2);
                    gifstate = 20;
                }
                if (gifstate == 31){

                    gif3.setGifResource(R.drawable.gif3);
                    gifstate = 30;
                }
                System.out.println("123");
            }

            @Override
            public void onFinish() {

            }
        };
    }


    private void setClick() {
        dialcon.setOnClickListener(this);
        gateway.setOnClickListener(this);
        light.setOnClickListener(this);
        computer.setOnClickListener(this);
        power.setOnClickListener(this);
        chehui.setOnClickListener(this);
        chehui1.setOnClickListener(this);
        ok.setOnClickListener(this);
        begin.setOnClickListener(this);
        diannao1.setOnClickListener(this);
        wangguan1.setOnClickListener(this);
        dianyuan1.setOnClickListener(this);
        guangzhao1.setOnClickListener(this);
        opower.setOnClickListener(this);
        ogateway.setOnClickListener(this);
        ocomputer.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    private void initview() {
        success = findViewById(R.id.success);
        opower = findViewById(R.id.opower);
        ocomputer = findViewById(R.id.ocomputer);
        ogateway = findViewById(R.id.ogateway);
        ma = findViewById(R.id.ma);
        lux = findViewById(R.id.lux);
        lux1 = findViewById(R.id.lux1);
        diannao1 = findViewById(R.id.diannao1);
        wangguan1 = findViewById(R.id.wangguan1);
        dianyuan1 = findViewById(R.id.dianyuan1);
        guangzhao1 = findViewById(R.id.guangzhao1);
        gateway = findViewById(R.id.gateway);
        light = findViewById(R.id.light);
        computer = findViewById(R.id.computer);
        power = findViewById(R.id.power);
        chehui = findViewById(R.id.chehui);
        chehui1 = findViewById(R.id.chehui1);
        ok = findViewById(R.id.ok);
        dial = findViewById(R.id.dial);
        dialcon = findViewById(R.id.dialcon);
        title = findViewById(R.id.hello);
        begin = findViewById(R.id.begin);
        gif3 = (GifView) findViewById(R.id.gifView);
        sharedPreferences = getSharedPreferences("auth",MODE_PRIVATE);
        String name = sharedPreferences.getString("name","123456");
        title.setText("                "+name+"  您好！欢迎来到轻互教学");
        dial.setText("");

    }

    @SuppressLint({"NonConstantResourceId", "WrongConstant"})
    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.dialcon:

                if (falg == 1){
                    dial.setVisibility(View.INVISIBLE);
                    falg=0;
                }else {
                    dial.setVisibility(View.VISIBLE);
                    falg =1;
                }

                break;


            case R.id.begin:
                dial.setVisibility(View.VISIBLE);
                duihua.start();


                break;

            case R.id.power:
                if (dyflag ==1){
                    dianyuan1.setVisibility(View.INVISIBLE);
                    dyflag = 0;
                }else {
                    dianyuan1.setVisibility(View.VISIBLE);
                    dyflag =1;
                }

                break;

            case R.id.computer:
                if (dnflag ==1){
                    diannao1.setVisibility(View.INVISIBLE);
                    dnflag = 0;
                }else {
                    diannao1.setVisibility(View.VISIBLE);
                    dnflag =1;
                }

                break;

            case R.id.light:
                if (gzflag ==1){
                    guangzhao1.setVisibility(View.INVISIBLE);
                    gzflag = 0;
                }else {
                    guangzhao1.setVisibility(View.VISIBLE);
                    gzflag =1;
                }

                break;

            case R.id.gateway:
                if (wgflag ==1){
                    wangguan1.setVisibility(View.INVISIBLE);
                    wgflag = 0;
                }else {
                    wangguan1.setVisibility(View.VISIBLE);
                    wgflag =1;
                }

                break;

            case R.id.ok:
                if (wgflag == 1 && dyflag ==1 && gzflag ==1 && dnflag==1){
                    dianyuan1.setVisibility(View.INVISIBLE);
                    diannao1.setVisibility(View.INVISIBLE);
                    guangzhao1.setVisibility(View.INVISIBLE);
                    wangguan1.setVisibility(View.INVISIBLE);
                    gif3.setGifResource(R.drawable.gif1);
                    gif3.setVisibility(View.VISIBLE);
                    ogateway.setVisibility(View.VISIBLE);
                    ocomputer.setVisibility(View.VISIBLE);
                    opower.setVisibility(View.VISIBLE);
                    ma.setVisibility(View.VISIBLE);
                    lux1.setVisibility(View.VISIBLE);
                    lux.setVisibility(View.VISIBLE);
                }else {

                    Toast.makeText(getApplicationContext(),"你没有对哦",0).show();
                }
                break;

            case R.id.opower:
                ma.setText("15ma");

                break;

            case R.id.ogateway:
                lux.setText("506lux");

                break;

            case R.id.ocomputer:
                if (lux.getText().equals("506lux")){
                    lux1.setText("506lux");
                }

                break;
        }

    }
}